<?php
require_once $ubi['SERVER']['SERVER_ROOT'] . '/dao/dbConnection.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/model/address.php';

class AddressDao {
	private $_db;

	public function __construct() {
		$this->_db = new DbConnection();
	}

	public function addAddress($address) {		
		$query = sprintf("CALL sp_AddAddress('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%d', '%d', '%d', @AddressId)",
					$address->getNumber(),
					$address->getStreet(),
					$address->getWard(),
					$address->getDistrict(),
					$address->getCity(),
					$address->getLatitude(),
					$address->getLongitude(),
					$address->getType(),
					$address->getLocation1(),
					$address->getLocation2(),
					$address->getLocation3());
					
		$result = $this->_db->executeQuery($query);
		
		if (!$result) {
		    echo 'Could not run query: ' . ' ' . $query . ' ' . mysql_error();
		    return 0;
		}
		
		$result = $this->_db->executeQuery('SELECT @AddressId');

		if (!$result) {
		    echo 'Could not run query: ' . mysql_error();
		    return 0;
		}
		
		if ($row = mysql_fetch_assoc($result)) {
			$id = $row['@AddressId'];
		}		
						
		mysql_free_result($result);
		
		return $id;
	}
	
	/*
	 * Delete address knows its id
	 * returns true if delete successfully
	 * returns false if can't delete
	 */
	public function deleteAddress($addressID) {		
		global $ubi;
		$query = sprintf("CALL sp_DeleteAddress('%s');", $addressID);
		
		$result = $this->_db->executeQuery($query);
		
		if (!$result) {
		    echo 'Could not run query: ' . ' ' . $query . ' ' . mysql_error();
		    return 0;
		}
				
		return false;
	}
	
	public function getAddress($addressID) {
		$query = "SELECT * FROM addresses WHERE AddressId = '" . $addressID . "'";
				
		$result = $this->_db->executeQuery($query);
		
		if (!$result) {
		    echo 'Could not run query: ' . ' ' . $query . ' ' . mysql_error();
		    return 0;
		}
		
		if ($row = mysql_fetch_assoc($result)) {			
			$add = AddressDao::getAddressFromFetchRow($row);
		}			
			
		mysql_free_result($result);	
		
		return $add;
	}	
	
	public function getAddressString($addressID) {
		$add = $this->getAddress($addressID);
		$str = $add->getNumber() . ' ' . $add->getStreet() . ' ' . $add->getWard() . ' ' . $add->getDistrict();
		return $str;
	}
	
	public function updateAddress(AddressModel $address) {	
		$query = sprintf("CALL sp_UpdateAddress('%d', '%s', '%s', '%s', '%s', '%s', '%d', '%d', '%d')", 
			$address->getAddressID(), $address->getCity(), $address->getDistrict(), 
			$address->getNumber(), $address->getStreet(), $address->getWard(),
			$address->getLocation1(), $address->getLocation2(), $address->getLocation3());			

		$result = $this->_db->executeQuery($query);

		if (!$result) {
		    echo 'Could not run query: ' . ' ' . $query . ' ' . mysql_error();
		    return;
		}

		return 1;
	}
	
	public static function getAddressFromFetchRow($row) {
		$address = new AddressModel();
		
		$address->setAddressID($row['AddressId']);
		$address->setCity($row['City']);
		$address->setDistrict($row['District']);
		$address->setLatitude($row['Latitude']);
		$address->setLongitude($row['Longitude']);
		$address->setNumber($row['Number']);
		$address->setStreet($row['Street']);
		$address->setType($row['Type']);
		$address->setWard($row['Ward']);
		$address->setLocation1($row['Location1']);
		$address->setLocation2($row['Location2']);
		$address->setLocation3($row['Location3']);
		
		return $address;
	}
}
?>